package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataActionMove extends DataAction implements ITag{
	public static String DEFAULT_ID="_ActionMove";
		DataPoint startPosition = new DataPoint();
		DataPoint position = new DataPoint();
		DataPoint endPosition = new DataPoint();
public DataActionMove(DataNode parent) {
	super(parent);

	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_POSITION))
		{
			startPosition = new DataPoint(TagModifier.POINT_TYPE_POINT);
	  		startPosition.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.POSITION))
		{
			position = new DataPoint(TagModifier.POINT_TYPE_POINT);
	  		position.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.END_POSITION))
		{
			endPosition = new DataPoint(TagModifier.POINT_TYPE_POINT);
	  		endPosition.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_MOVE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_POSITION))
		{
	  		startPosition.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.POSITION))
		{
	  		position.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.END_POSITION))
		{
	  		endPosition.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_MOVE,""+element.getStartLineNo()} );
		}
	}
}
public DataAction getClone() {

	DataActionMove newActionMove = new DataActionMove(parent);

	super.getClonedElements(newActionMove);

	newActionMove.startPosition = startPosition.getClone();
	newActionMove.position = position.getClone();
	newActionMove.endPosition = endPosition.getClone();

	return newActionMove;
}
public DataAction getClone(String path) {

	DataActionMove newActionMove = new DataActionMove(parent);

	GrfxMLStoreProcessor.setCloneName( path,this,newActionMove );

	super.getClonedElements(path,newActionMove);

	newActionMove.startPosition = startPosition.getClone(path);
	newActionMove.position = position.getClone(path);
	newActionMove.endPosition = endPosition.getClone(path);

	return newActionMove;
}
public DataPoint getEndPosition() {
	return endPosition;
}
public DataPoint getPosition() {
	return position;
}
public DataPoint getStartPosition() {
	return startPosition;
}
/**
 * process method comment.
 */
protected void process()
{
 try{
	int newX = startPosition.getValueX();
	int diffX = endPosition.getValueX() - newX;
	newX += currentStep.getValue() * diffX / steps.getValue();
	position.setValueX(newX, this);
	int newY = startPosition.getValueY();
	int diffY = endPosition.getValueY() - newY;
	newY += currentStep.getValue() * diffY / steps.getValue();
	position.setValueY(newY, this);
 }catch(LockException le){}
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(startPosition,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(position,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(endPosition,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {

	replace(oldObj,newObj,false);

}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	super.replace(oldObj,newObj);

	if(startPosition==oldObj)
		startPosition=(DataPoint)newObj;

	if(position==oldObj)
		position=(DataPoint)newObj;

	if(endPosition==oldObj)
		endPosition=(DataPoint)newObj;
}
public void resetChanged() {
	super.resetChanged();
	startPosition.resetChanged();
	position.resetChanged();
	endPosition.resetChanged();
}
protected void setEndPosition(DataPoint newEndPosition) {
	endPosition = newEndPosition;
}
protected void setPosition(DataPoint newPosition) {
	position = newPosition;
}
protected void setStartPosition(DataPoint newStartPosition) {
	startPosition = newStartPosition;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += startPosition.togrfxML(Token.START_POSITION);
	xml += position.togrfxML(Token.POSITION);
	xml += endPosition.togrfxML(Token.END_POSITION);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString() {
	if (GrfxMLEngine.DEBUG)
		return (togrfxML(DEFAULT_ID.substring(1)));
	String xml = "<" + DEFAULT_ID.substring(1) + "/>";
	return xml;
}
}
